package myky.com.doctorapp.adapter

import android.graphics.Color
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import myky.com.doctorapp.R
import myky.com.doctorapp.entity.RemoteDetailsEntity
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

/**
 * Description:查看远程病历图片集合adapter
 *
 * @author wuyangwuyang
 * Created by 2019/3/12 on 9:40
 */
class RemoteStatusAdapter(val imageList:List<RemoteDetailsEntity.PayLoadBean.DetailBean.RemoteLogsBean>):BaseQuickAdapter<RemoteDetailsEntity.PayLoadBean.DetailBean.RemoteLogsBean,BaseViewHolder>(R.layout.remote_status_adapter,imageList){


    override fun convert(helper: BaseViewHolder, item: RemoteDetailsEntity.PayLoadBean.DetailBean.RemoteLogsBean) {
        var position = helper.position
        if (item.createtime!=null){
            helper.setText(R.id.new_remote_state_tv2,item.createtime)
        }
        if (imageList.size - 1 == position) {
            if (imageList.size == 1) {
                helper.getView<TextView>(R.id.new_remote_state_sx2).visibility=View.GONE
            }
            helper.getView<TextView>(R.id.new_remote_state_sx).visibility=View.GONE
        } else if (position == 0) {
            helper.getView<TextView>(R.id.new_remote_state_sx2).visibility=View.GONE

        }

        val tv_status = helper.getView<TextView>(R.id.new_remote_state_tv1)
        val iv_status = helper.getView<ImageView>(R.id.new_remote_state_iv)
        if (item.currStatusText == null) {
            tv_status.text=mContext.resources.getStringArray(R.array.remote_currAppStatus)[item.currStatus]
        } else {
            tv_status.text=item.currStatusText
        }

        when(item.currStatus){

            0->{
                if (item.currStatusText == null) {
                    tv_status.text=mContext.resources.getStringArray(R.array.remote_currAppStatus)[item.currStatus]
                    iv_status.setImageResource(R.mipmap.icon_hztjblzl)
                } else {
                    helper.getView<TextView>(R.id.new_remote_state_tv2).visibility=View.GONE
                    tv_status.text=item.currStatusText
                    tv_status.setTextColor(Color.parseColor("#9a9a9a"))
                    iv_status.setImageResource(R.drawable.icon_jyjxzt)
                }
            }
            1->{

            }
            2->{
                tv_status.text = mContext.resources.getString(R.string.remote_status_cancel)
                tv_status.setTextColor(Color.parseColor("#ff0000"))
                iv_status.setImageResource(R.drawable.icon_tszt)
            }
            3->{
                tv_status.text=mContext.resources.getStringArray(R.array.remote_currAppStatus)[3]
                tv_status.setTextColor(Color.parseColor("#ff0000"))
                iv_status.setImageResource(R.drawable.icon_tszt)

            }
            4->{

            }
            5->{
                tv_status.text=mContext.resources.getStringArray(R.array.remote_currAppStatus)[5]
                tv_status.setTextColor(Color.parseColor("#ff0000"))
                iv_status.setImageResource(R.drawable.icon_tszt)
            }
            6->{

            }
            7->{

            }
            8->{
                val time = getRandomTime(item.createtime)
                if (time != null) {
                    helper.setText(R.id.new_remote_state_tv2, time)
                    tv_status.text=mContext.resources.getStringArray(R.array.remote_currAppStatus)[9]
                }
            }
            9->{
                tv_status.setTextColor(Color.parseColor("#ff0000"))
                iv_status.setImageResource(R.drawable.icon_tszt)
                if (item.cause == 5) {
                    tv_status.text=mContext.resources.getStringArray(R.array.remote_currAppStatus)[10]
                }
            }
        }
    }

    /**
     * 开方中和正在开方中本地时间比较5分钟变成已开方
     */
    fun getRandomTime(time: String): String? {


        var backTime: String? = null
        val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        try {
            val createTime = dateFormat.parse(time)
            val currentTime = Date(System.currentTimeMillis())
            val diff = currentTime.time - createTime.time
            Log.i("out", "时间差...$diff")

            if (diff > 300000) {
                backTime = dateFormat.format(Date(createTime.time + 300000))

            }
        } catch (e: ParseException) {
            e.printStackTrace()

        }


        return backTime
    }
}
